# WebSocket聊天应用本地部署脚本 (Windows PowerShell)
# 使用方法: .\deploy-local.ps1

Write-Host "=== WebSocket聊天应用本地部署开始 ===" -ForegroundColor Green

# 检查必要文件
Write-Host "检查部署文件..." -ForegroundColor Yellow
if (!(Test-Path "Dockerfile.publish")) {
    Write-Host "❌ 错误: Dockerfile.publish 文件不存在" -ForegroundColor Red
    exit 1
}

if (!(Test-Path "docker-compose.publish.yml")) {
    Write-Host "❌ 错误: docker-compose.publish.yml 文件不存在" -ForegroundColor Red
    exit 1
}

if (!(Test-Path "publish")) {
    Write-Host "❌ 错误: publish 目录不存在" -ForegroundColor Red
    exit 1
}

if (!(Test-Path "publish\hao-Chat.Api.dll")) {
    Write-Host "❌ 错误: publish\hao-Chat.Api.dll 文件不存在" -ForegroundColor Red
    exit 1
}

Write-Host "✅ 所有必要文件检查通过" -ForegroundColor Green

# 检查Docker是否运行
Write-Host "检查Docker状态..." -ForegroundColor Yellow
try {
    docker info | Out-Null
    Write-Host "✅ Docker服务正常运行" -ForegroundColor Green
} catch {
    Write-Host "❌ Docker服务未运行，请启动Docker Desktop" -ForegroundColor Red
    exit 1
}

# 停止现有容器
Write-Host "停止现有容器..." -ForegroundColor Yellow
docker compose -f docker-compose.publish.yml down 2>$null

# 构建并启动容器
Write-Host "构建并启动容器..." -ForegroundColor Yellow
docker compose -f docker-compose.publish.yml up -d --build

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 容器构建失败" -ForegroundColor Red
    exit 1
}

# 等待容器启动
Write-Host "等待容器启动..." -ForegroundColor Yellow
Start-Sleep -Seconds 10

# 检查容器状态
Write-Host "检查容器状态..." -ForegroundColor Yellow
$containerStatus = docker ps --filter "name=websocket-chat-app" --format "table {{.Names}}\t{{.Status}}"

if ($containerStatus -match "websocket-chat-app") {
    Write-Host "✅ 容器启动成功" -ForegroundColor Green
    
    # 显示容器详细信息
    Write-Host "`n=== 容器信息 ===" -ForegroundColor Cyan
    docker ps --filter "name=websocket-chat-app" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
    
} else {
    Write-Host "❌ 容器启动失败，查看详细信息:" -ForegroundColor Red
    
    Write-Host "`n=== 容器日志 ===" -ForegroundColor Yellow
    docker compose -f docker-compose.publish.yml logs
    
    Write-Host "`n=== 所有容器状态 ===" -ForegroundColor Yellow
    docker ps -a --filter "name=websocket-chat-app"
    
    exit 1
}

# 测试应用响应
Write-Host "`n测试应用响应..." -ForegroundColor Yellow
Start-Sleep -Seconds 3

try {
    $response = Invoke-WebRequest -Uri "http://localhost:8083" -TimeoutSec 10 -UseBasicParsing
    if ($response.StatusCode -eq 200) {
        Write-Host "✅ 应用响应正常" -ForegroundColor Green
    } else {
        Write-Host "⚠️ 应用响应异常，状态码: $($response.StatusCode)" -ForegroundColor Yellow
    }
} catch {
    Write-Host "⚠️ 无法连接到应用，可能仍在启动中" -ForegroundColor Yellow
    Write-Host "错误信息: $($_.Exception.Message)" -ForegroundColor Gray
}

# 显示部署结果
Write-Host "`n=== 部署完成 ===" -ForegroundColor Green
Write-Host "本地访问地址: http://localhost:8083" -ForegroundColor Cyan
Write-Host "WebSocket端点: ws://localhost:8083/ws" -ForegroundColor Cyan
Write-Host ""
Write-Host "管理命令:" -ForegroundColor Yellow
Write-Host "  查看容器状态: docker ps" -ForegroundColor Gray
Write-Host "  查看应用日志: docker compose -f docker-compose.publish.yml logs" -ForegroundColor Gray
Write-Host "  停止应用: docker compose -f docker-compose.publish.yml down" -ForegroundColor Gray
Write-Host "  重启应用: docker compose -f docker-compose.publish.yml restart" -ForegroundColor Gray